package svc

import (
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/config"
	"gitee.com/liuxuezhan/ar-platform/base-common/rpc"
	analysis "gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/client"
	clientConfig "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/config"
	clientPayRecharge "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/payrecharge"
	clientPayWithdraw "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/paywithdraw"
	clientTenantChannel "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/tenantchannel"
	clientUser "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/user"
	clientUserChannel "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/userchannel"
	clientUserWash "gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/client/userwash"
	gameManageClient "gitee.com/liuxuezhan/ar-platform/rpc-client/gameManageClient/client/game"
	UserAuthClient "gitee.com/liuxuezhan/ar-platform/rpc-client/userAuthClient/client/auth"
	"github.com/zeromicro/go-zero/core/logx"

	"gitee.com/liuxuezhan/ar-platform/base-common/rpc/interceptors"
	agentClient "gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/client"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/authRoleClient/client/role"
	loginUser "gitee.com/liuxuezhan/ar-platform/rpc-client/loginClient/client"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/redPacketClient/redpacket"
	safeSetting "gitee.com/liuxuezhan/ar-platform/rpc-client/safeBoxClient/client/setting"
	safeTransfer "gitee.com/liuxuezhan/ar-platform/rpc-client/safeBoxClient/client/transfer"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/secretClient/client/secret"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/activityinfo"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/agreement"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/appconfig"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/carouselmap"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/customerservicegroup"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/dataview"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/event"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/feedback"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/grandprize"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/interfaceconfig"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/ipwhite"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/message"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config         config.Config
	UserServiceRPC userservice.UserService
	SettingService SettingService
	// // 保险箱rpc服务
	SafeBoxRpcService SafeBoxRpcService
	AssetsRpc         AssetsRpc
	LoginRpc          loginUser.User
	AgentRPC          agentClient.Agent
	AuthRoleRpc       role.Role
	SecretRPC         secret.Secret
	UserAuth          UserAuthClient.Auth
	Analysis          analysis.Analysis
	GameManageRpc     gameManageClient.Game
	RedPacketRpc      redpacket.RedPacket
}

func NewServiceContext(c config.Config) *ServiceContext {
	// 自定义拦截器
	loginRPC := zrpc.MustNewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.LoginRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	loginRPCClient := loginUser.NewUser(loginRPC)

	userServiceRPC, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.UserRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}
	userServiceClient := userservice.NewUserService(userServiceRPC)

	redPacketRPC, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.RedPacketRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}
	redPacketClient := redpacket.NewRedPacket(redPacketRPC)

	// userAuthRpc, err := zrpc.NewClient(zrpc.RpcClientConf{
	//	Target: rpc.GenRpcTarget(c.UserAuthRpc),
	// }, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	// if err != nil {
	//	logx.Error(err)
	// }
	// userAuthClient := UserAuthClient.NewAuth(userAuthRpc)

	AssetsClient, err := zrpc.NewClient(
		zrpc.RpcClientConf{
			Target: rpc.GenRpcTarget(c.AssetsRPC),
		},
		zrpc.WithUnaryClientInterceptor(
			interceptors.ClientErrorInterceptor(c.Name, c.Name),
		),
	)
	if err != nil {
		logx.Error(err)
	}

	agentRPC, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.AgentRPC),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}
	agentRPCCient := agentClient.NewAgent(agentRPC)

	gameRPC, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.GameRpc),
	})
	gameRPCClient := gameManageClient.NewGame(gameRPC)

	secretRPC := rpc.MustNewClient(rpc.Config{
		Host:     c.SecretRPC,
		AppName:  c.Name,
		Business: c.Name,
	})

	analysisRPC, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.AnalysisRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}
	analysisRPCClient := analysis.NewAnalysis(analysisRPC)
	return &ServiceContext{
		Config: c,
		AssetsRpc: AssetsRpc{
			Config:        clientConfig.NewConfig(AssetsClient),
			PayRecharge:   clientPayRecharge.NewPayRecharge(AssetsClient),
			PayWithdraw:   clientPayWithdraw.NewPayWithdraw(AssetsClient),
			TenantChannel: clientTenantChannel.NewTenantChannel(AssetsClient),
			User:          clientUser.NewUser(AssetsClient),
			UserChannel:   clientUserChannel.NewUserChannel(AssetsClient),
			UserWash:      clientUserWash.NewUserWash(AssetsClient),
		},
		LoginRpc:       loginRPCClient,
		UserServiceRPC: userServiceClient,
		SettingService: settingRpcService(c),
		// 保险箱rpc服务
		SafeBoxRpcService: safeBoxRpcService(c),
		AgentRPC:          agentRPCCient,
		AuthRoleRpc:       authRoleService(c),
		SecretRPC:         secret.NewSecret(secretRPC),
		// UserAuth:          userAuthClient,
		Analysis:      analysisRPCClient,
		GameManageRpc: gameRPCClient,
		RedPacketRpc:  redPacketClient,
	}
}

type AssetsRpc struct {
	Config        clientConfig.Config
	PayRecharge   clientPayRecharge.PayRecharge
	PayWithdraw   clientPayWithdraw.PayWithdraw
	TenantChannel clientTenantChannel.TenantChannel
	User          clientUser.User
	UserChannel   clientUserChannel.UserChannel
	UserWash      clientUserWash.UserWash
}
type SettingService struct {
	AgreementRpc    agreement.Agreement
	AppConfig       appconfig.AppConfig
	Feedback        feedback.Feedback
	GrandPrize      grandprize.GrandPrize
	IpWhite         ipwhite.IpWhite
	Activity        activityinfo.ActivityInfo
	CarouselMap     carouselmap.CarouselMap
	InterfaceConfig interfaceconfig.InterfaceConfig
	DataView        dataview.DataView
	Message         message.Message
	CustomerService customerservicegroup.CustomerServiceGroup
	EventService    event.Event
}

func settingRpcService(c config.Config) SettingService {
	settingRpc, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.SettingRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}
	return SettingService{
		AgreementRpc:    agreement.NewAgreement(settingRpc),
		AppConfig:       appconfig.NewAppConfig(settingRpc),
		Feedback:        feedback.NewFeedback(settingRpc),
		GrandPrize:      grandprize.NewGrandPrize(settingRpc),
		IpWhite:         ipwhite.NewIpWhite(settingRpc),
		Activity:        activityinfo.NewActivityInfo(settingRpc),
		CarouselMap:     carouselmap.NewCarouselMap(settingRpc),
		InterfaceConfig: interfaceconfig.NewInterfaceConfig(settingRpc),
		DataView:        dataview.NewDataView(settingRpc),
		Message:         message.NewMessage(settingRpc),
		CustomerService: customerservicegroup.NewCustomerServiceGroup(settingRpc),
		EventService:    event.NewEvent(settingRpc),
	}
}

// SafeBoxRpcService SafeBoxService 保险箱rpc服务
type SafeBoxRpcService struct {
	Setting  safeSetting.Setting
	Transfer safeTransfer.Transfer
}

// safeBoxRpcService 创建保险箱rpc服务
func safeBoxRpcService(c config.Config) SafeBoxRpcService {
	safeBoxRpc, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.SafeBoxRpc),
	})
	if err != nil {
		logx.Error(err)
	}
	return SafeBoxRpcService{
		Setting:  safeSetting.NewSetting(safeBoxRpc),
		Transfer: safeTransfer.NewTransfer(safeBoxRpc),
	}
}

func authRoleService(c config.Config) role.Role {
	rpcCli, err := zrpc.NewClient(zrpc.RpcClientConf{
		Target: rpc.GenRpcTarget(c.AuthRoleRpc),
	}, zrpc.WithUnaryClientInterceptor(interceptors.ClientErrorInterceptor(c.Name, c.Name)))
	if err != nil {
		logx.Error(err)
	}

	return role.NewRole(rpcCli)
}
